/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entity.OrderForm;
import entity.OrderInfo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import util.DatabaseUtil;

public class OrderDao {
    
    public List<OrderForm> getAllOrderForm(int memberId){    //根据用户ID获取用户所有订单
        List<OrderForm> list=new ArrayList<>();
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("select * from orderform where memberid=?");
            ps.setInt(1,memberId);
            rs=ps.executeQuery();
            while(rs.next()){
                list.add(new OrderForm(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getString(4),rs.getBoolean(5),rs.getInt(6)));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps, rs);
        }
        return list;
    }
    
    public OrderForm getOrderFormById(int id){    //根据订单号获取订单
        OrderForm orderform=null;
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("select * from orderform where id=?");
            ps.setInt(1,id);
            rs=ps.executeQuery();
            while(rs.next()){
                orderform=new OrderForm(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getString(4),rs.getBoolean(5),rs.getInt(6));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps, rs);
        }
        return orderform;
    }
    
    
     public List<OrderForm> getOrderFormByBookName(int memberId,String bookName){    //根据书名获取用户订单
        List<OrderForm> list=new ArrayList<>();
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("select * from orderform where memberid=? and id in"
                    + "(select orderid from orderinfo where bookid in"
                    + "(select id from book where bookname like ?))");
            ps.setInt(1,memberId);
            ps.setString(2,"%"+bookName+"%");
            rs=ps.executeQuery();
            while(rs.next()){
                list.add(new OrderForm(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getString(4),rs.getBoolean(5),rs.getInt(6)));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps, rs);
        }
        return list;
    }
    
    
    public Boolean addOrderForm(int memberId,int addressId){      //为用户添加订单
        int i=0;
        Connection con=null;
        PreparedStatement ps=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("insert into orderform(memberid,addressid) values(?,?)");
            ps.setInt(1,memberId);
            ps.setInt(2,addressId);
            i=ps.executeUpdate();
            if(i!=0){
                return true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps,null);
        }
        return false;
    }
    
    public Boolean addOrderInfo(int orderId,int bookId,int count){      //添加订单信息
        int i=0;
        Connection con=null;
        PreparedStatement ps=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("insert into orderinfo values(?,?,?)");
            ps.setInt(1,orderId);
            ps.setInt(2,bookId);
            ps.setInt(3, count);
            i=ps.executeUpdate();
            if(i!=0){
                return true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps,null);
        }
        return false;
    }
    
    public List<OrderInfo> getOrderInfo(int orderId){   //根据订单号获取订单信息
        List<OrderInfo> list=new ArrayList<>();
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("select * from orderinfo where orderid=?");
            ps.setInt(1,orderId);
            rs=ps.executeQuery();
            while(rs.next()){
                list.add(new OrderInfo(rs.getInt(2),rs.getInt(3)));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps,rs);
        }
        return list;
    }
    
    public Boolean checkOrderForm(int id){      //确认收货
        int i=0;
        Connection con=null;
        PreparedStatement ps=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("update orderform set ischeck=1 where id=?");
            ps.setInt(1,id);
            i=ps.executeUpdate();
            if(i!=0){
                return true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps,null);
        }
        return false;
    }
    
    public Boolean rateOrderForm(int id,int rate){      //评价订单
        int i=0;
        Connection con=null;
        PreparedStatement ps=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("update orderform set rate=? where id=?");
            ps.setInt(1,rate);
            ps.setInt(2,id);
            i=ps.executeUpdate();
            if(i!=0){
                return true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps,null);
        }
        return false;
    }
    
    
    public List<OrderForm> getCheckedOrder(int memberId){    //获取确认收货的订单
        List<OrderForm> list=new ArrayList<>();
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("select * from orderform where memberid=? and ischeck=1");
            ps.setInt(1,memberId);
            rs=ps.executeQuery();
            while(rs.next()){
                list.add(new OrderForm(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getString(4),rs.getBoolean(5),rs.getInt(6)));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps, rs);
        }
        return list;
    }
    public List<OrderForm> getUncheckedOrder(int memberId){    //获取未确认收货的订单
        List<OrderForm> list=new ArrayList<>();
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("select * from orderform where memberid=? and ischeck=0");
            ps.setInt(1,memberId);
            rs=ps.executeQuery();
            while(rs.next()){
                list.add(new OrderForm(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getString(4),rs.getBoolean(5),rs.getInt(6)));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps, rs);
        }
        return list;
    }
    
    public List<OrderForm> getRatedOrder(int memberId){    //获取已评价的订单
        List<OrderForm> list=new ArrayList<>();
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("select * from orderform where memberid=? and rate!=0");
            ps.setInt(1,memberId);
            rs=ps.executeQuery();
            while(rs.next()){
                list.add(new OrderForm(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getString(4),rs.getBoolean(5),rs.getInt(6)));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps, rs);
        }
        return list;
    }
    
    public List<OrderForm> getUnratedOrder(int memberId){    //获取未评价的订单
        List<OrderForm> list=new ArrayList<>();
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("select * from orderform where memberid=? and rate=0");
            ps.setInt(1,memberId);
            rs=ps.executeQuery();
            while(rs.next()){
                list.add(new OrderForm(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getString(4),rs.getBoolean(5),rs.getInt(6)));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps, rs);
        }
        return list;
    }
    
    public List<OrderForm> getWeekOrder(int memberId){    //获取一周内的订单
        List<OrderForm> list=new ArrayList<>();
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("select * from orderform where memberid=? and datediff(week,orderdate,getdate())=0");
            ps.setInt(1,memberId);
            rs=ps.executeQuery();
            while(rs.next()){
                list.add(new OrderForm(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getString(4),rs.getBoolean(5),rs.getInt(6)));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps, rs);
        }
        return list;
    }
    public List<OrderForm> getMonthOrder(int memberId){    //获取一个月内的订单
        List<OrderForm> list=new ArrayList<>();
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("select * from orderform where memberid=? and datediff(month,orderdate,getdate())=0");
            ps.setInt(1,memberId);
            rs=ps.executeQuery();
            while(rs.next()){
                list.add(new OrderForm(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getString(4),rs.getBoolean(5),rs.getInt(6)));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps, rs);
        }
        return list;
    }
    
    public List<OrderForm> getHalfyearOrder(int memberId){    //获取半年内的订单
        List<OrderForm> list=new ArrayList<>();
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("select * from orderform where memberid=? and datediff(month,orderdate,getdate())<=6");
            ps.setInt(1,memberId);
            rs=ps.executeQuery();
            while(rs.next()){
                list.add(new OrderForm(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getString(4),rs.getBoolean(5),rs.getInt(6)));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps, rs);
        }
        return list;
    }
}
